<template>
  <div style="width: 100%; height: 100%">
    <div
      style="
        display: flex;
        justify-content: space-between;
        height: 60px;
        box-sizing: border-box;
        border-bottom: 1px solid #f1f1f1;
      "
    >
      <div style="font-size: 20px; padding: 15px; cursor: pointer; font-weight: bold" @click="goBack">
        {{ designer.global.appName }}
      </div>
      <div v-if="designer.global.pageType === 2" style="display: flex; height: 100%">
        <div
          :class="{ 'design-type': true, 'design-type-selected': designType === 'page-design' }"
          @click="designType = 'page-design'"
        >
          页面设计
        </div>
        <div
          :class="{ 'design-type': true, 'design-type-selected': designType === 'flow-design' }"
          @click="designType = 'flow-design'"
        >
          流程设定
        </div>
      </div>
      <div style="padding: 15px">
        <ElButton type="primary" @click="handleSave">
          <template #icon>
            <Icon icon="plus"></Icon>
          </template>
          <span>保存</span>
        </ElButton>
      </div>
    </div>
    <div v-show="designType === 'page-design'" style="height: calc(100% - 60px)">
      <div style="display: flex; height: 100%">
        <div style="width: 320px; height: calc(100vh - 60px); overflow: auto">
          <SettingPanel :designer="designer"></SettingPanel>
        </div>
        <div
          style="
            flex: 1;
            padding: 15px;
            box-sizing: border-box;
            border-left: 1px dotted #cccccc;
            border-right: 1px dotted #cccccc;
            contain: paint;
          "
        >
          <MicroRender :designer="designer" />
        </div>
        <div style="width: 350px">
          <PropertyPanel :designer="designer"></PropertyPanel>
        </div>
      </div>
    </div>
    <div v-show="designType === 'flow-design'" style="height: calc(100% - 60px)">
      <Flow ref="flowRef" :data="processData" />
    </div>
  </div>
</template>

<script setup lang="ts" name="MicroDesign">
import { computed, provide, ref, watch } from "vue";
import { ElButton } from "element-plus";
import SettingPanel from "./SettingPanel/index.vue";
import PropertyPanel from "./PropertyPanel/index.vue";
import MicroRender from "../Render/index.vue";
import Flow from "../Flow/index.vue";
import microPageApi from "@/views/micro/page/api";
import microProcessApi from "@/views/micro/api/process.api";
import { Icon } from "@/components/Icon";
import { useRouter } from "vue-router";
const props = defineProps({
  appId: {
    type: Number
  }
});
const designType = ref("page-design");
const designer = ref({
  widget: {},
  global: {
    appId: props.appId,
    appName: undefined,
    pageType: undefined,
    design: true,
    entityId: undefined,
    pageList: []
  }
});
provide("designer", designer);
const processData = ref({});
const flowRef = ref();
watch(
  () => designer.value.global.appId,
  async value => {
    if (value) {
      const res = await microProcessApi.queryByAppId(value as Number);
      if (res) {
        processData.value = JSON.parse(res.processJson);
        return;
      }
    }
    processData.value = {};
  },
  {
    immediate: true
  }
);
const getFlowData = () => {
  const result = flowRef.value.validate();
  if (result.valid) {
    return flowRef.value.getResult();
  }
  throw new Error(result.messages);
};
const pageList = computed(() => designer.value.global.pageList);
const router = useRouter();
const goBack = () => {
  router.back();
};
const handleSave = async () => {
  if (designType.value === "page-design") {
    await microPageApi.save({
      appId: props.appId,
      pageList: pageList.value.map(page => {
        const res = Object.assign({}, page);
        res.pageJson = JSON.stringify(page.pageJson);
        return res;
      })
    });
  } else if (designType.value === "flow-design") {
    const flowData = getFlowData();
    flowData.processId = "process" + designer.value.global.appId;
    flowData.processName = designer.value.global.appName;
    const data = {
      processId: flowData.processId,
      processName: flowData.processName,
      appId: designer.value.global.appId,
      processJson: JSON.stringify(flowData)
    };
    await microProcessApi.save(data);
  }
};
</script>

<style lang="scss" scoped>
.design-type {
  padding: 15px 0;
  margin-left: 30px;
  cursor: pointer;
}
.design-type-selected {
  color: #009688;
  border-bottom: 4px solid #009688;
}
</style>
